package com.jm.bishe.service.Impl;

import com.jm.bishe.entity.*;
import com.jm.bishe.mapper.AdminMapper;
import com.jm.bishe.mapper.UserMapper;
import com.jm.bishe.service.AdminService;
import com.jm.bishe.utils.JwtTokenUtil;
import com.jm.bishe.utils.MD5Util;
import com.jm.bishe.utils.VoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Slf4j
public class AdminServiceImpl implements AdminService {


    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PermissionServiceImpl permissionService;

    @Override
    public Result login(String adminPhoneNumber, String adminPassWord) {
        Admin admin = adminMapper.queryAdminByAdminPhoneNumber(adminPhoneNumber);
        if (admin!=null && !VoUtil.allFieldIsNULL(admin)){
            if (admin.getAdminPhoneNumber() != null && !admin.getAdminPhoneNumber().equals("")){
                if (!admin.getAdminPhoneNumber().equals(adminPhoneNumber)
                        && !admin.getAdminPassWord().equals(adminPassWord)){
                    return Result.falure(1009,"管理员用户名或密码错误",null);
                }
                return Result.success("管理员登录成功", JwtTokenUtil.getToken(String.valueOf(admin.getAdminId())));
            }
        }
        return Result.falure(2019,"没有该管理员",null);
    }


    @Override
    public Result addUser(UserRegister userRegister, String userId) {
        if (permissionService.queryPermission(userId,2)) {
            return Result.falure(2029,"你没有该权限",null);
        }
        if (userRegister ==null ){
            return Result.falure(1003,"请正确输入用户信息",null);
        }
        List<User> users = userMapper.queryUserByPhoneNumber(userRegister.getPhoneNumber());
        if (users.size() !=0 ){
            return Result.falure(1008,"手机号已被注册",null);
        }
        try {
            int i = userMapper.addUser(new User(
                    System.currentTimeMillis(), MD5Util.convertMD5(MD5Util.desEncrypt(userRegister.getPassWord())),
                    userRegister.getRealName(),
                    new SimpleDateFormat("yyyy-MM-dd").parse(userRegister.getBornDate()),
                    userRegister.getPhoneNumber(), userRegister.getSex(),
                    userRegister.getIDNumber(), 0, 0));
            if (i == 0){
                return Result.falure(1001,"未知错误,请联系管理员",null);
            }
            log.info(userId+"添加了新用户:"+userRegister.getRealName());
            return Result.success("添加成功",JwtTokenUtil.getToken(userId));
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.falure(1002,"日期格式错误",null);
        }
    }

    @Override
    public Result setVip(String phoneNumber, String userId) {
        if (permissionService.queryPermission(userId,2)) {
            return Result.falure(2029,"你没有该权限",null);
        }
        List<User> users = userMapper.queryUserByPhoneNumber(phoneNumber);
        if (users.size()==0){
            return Result.falure(2009,"用户不存在",null);
        }
        if (users.get(0).getIsVip()==1){
            return Result.falure(2011,"该用户已是Vip",null);
        }
        int i = userMapper.setVip(phoneNumber);
        if (i==0){
            return Result.falure(1001,"未知错误,请联系管理员",null);
        }
        return Result.success("设置Vip成功",JwtTokenUtil.getToken(userId));
    }

    @Override
    public Result updateVipCount(String phoneNumber,String vipCount, String userId) {
        if (permissionService.queryPermission(userId,2)) {
            return Result.falure(2029,"你没有该权限",null);
        }
        List<User> users = userMapper.queryUserByPhoneNumber(phoneNumber);
        if (users.size()==0){
            return Result.falure(2009,"用户不存在",null);
        }
        int i = userMapper.updateVipCount(phoneNumber, vipCount);
        if (i==0){
            return Result.falure(1001,"更新失败,请联系管理员",null);
        }
        return Result.success("更新成功",JwtTokenUtil.getToken(userId));
    }

    @Override
    public Result queryUserByPhoneNumber(String phoneNumber, String userId) {
        if (permissionService.queryPermission(userId,2)) {
            return Result.falure(2029,"你没有该权限",null);
        }
        List<User> users = userMapper.queryUserByPhoneNumber(phoneNumber);
        if (users.size()==0){
            return Result.falure(2009,"用户不存在",null);
        }
        User user = users.get(0);
        user.setPassWord(null);
        user.setUserId(null);
        return Result.success("查询成功",user);
    }

    @Override
    public Result queryUserByVip(int isVip,String userId) {
        if (permissionService.queryPermission(userId,2)) {
            return Result.falure(2029,"你没有该权限",null);
        }
        List<User> users = userMapper.queryUserByVip(isVip);
        if (users.size()==0){
            return Result.falure(2009,"用户不存在",null);
        }
        for (User user : users) {
            user.setPassWord(null);
            user.setUserId(null);
        }
        return Result.success("查询成功",users);

    }

    @Override
    public Result updateUser(UserUpdate userUpdate, String userId){
        if (permissionService.queryPermission(userId,2)) {
            return Result.falure(2029,"你没有该权限",null);
        }
        if (!VoUtil.allFieldIsNULL(userUpdate)){
            int i = userMapper.updateUser(userUpdate);
            if (i==1){
                return Result.success("更新成功",JwtTokenUtil.getToken(userId));
            }
            else return Result.falure(1001,"数据库错误,请联系管理员",null);
        }
        return Result.falure(1003,"数据格式错误，请正确输入",null);
    }

    @Override
    public Result queryAllUser(String userId) {
        if (permissionService.queryPermission(userId,2)) {
            return Result.falure(2029,"你没有该权限",null);
        }else {
            List<User> users = userMapper.queryAllUser();
            for (User user : users) {
                user.setPassWord(null);
                user.setUserId(null);
            }
            return Result.success("查询成功",users);
        }
    }



}
